package com.kanshu.ksgb.fastread.doudou.module.personal.utils;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.StorageUtils;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.ConfigBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserData;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterModel;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者    wcy
 * 时间    2018/12/4 14:44
 * 文件    doudou
 * 描述
 */
public class UserInfoUtils {
    /**
     * 获取用户基本信息
     * @param callback
     */
    public static void getUserBaseInfo(INetCommCallback<UserData> callback) {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        Map<String, String> params = new HashMap<>();
        params.put("get_master_info", "1");
        params.put("get_account_info", "1");
        params.put("get_phone_info", "1");
        params.put("get_bind_info", "1");
        helper.createService(PersonCenterService.class)
                .getUserBaseInfo(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<BaseResult<UserData>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<UserData> userInfoBaseResult) {
                if (userInfoBaseResult.result.data != null && callback != null) {
                    callback.onResponse(userInfoBaseResult.result.data);
                    StorageUtils.setPreference(Xutils.getContext(), Constants.SP_NAME, Constants.USER_BIND_PHONE + userInfoBaseResult.result.data.user_id, userInfoBaseResult.result.data.phone);
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                if (callback != null) {
                    callback.onError(-1, "net error");
                }
            }

            @Override
            public void onComplete() {
            }
        });
    }

    /**
     * __flush_cache配置获取
     * @param callback
     */
    public static void getConfig(INetCommCallback<ConfigBean> callback) {
        PersonCenterModel model = new PersonCenterModel();
        model.getConfig(callback);
    }
}
